//学习flutter路由相关

import 'package:flutter/material.dart';
import 'package:flutterapp/page/OrderMealIndexPage.dart';
import 'package:flutterapp/page/OrderMealRecordPage.dart';
import 'package:flutterapp/stream/StreamPage.dart';

import 'NavigationBar.dart';

class FirstRouterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("FirstPage")),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                //页面入栈的操作 //MaterialPageRoute
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (BuildContext context) => SecondRouterPage()));
              },
              child: Text("open page"),
            ),
            RaisedButton(
              onPressed: () {
                //第二种的启动方式（路由表的方式）
                Navigator.pushNamed(context, "/second_router_page");
              },
              child: Text("open Routing table page"),
            ),
            RaisedButton(
              onPressed: () async {
                //接受返回的参数和以前的一样
                //路由表启动的两种方式
//            Navigator.pushNamed(context, "/router_map_firstPage",
//                arguments: "first Router table page arg");
                var arg = await Navigator.of(context).pushNamed(
                    "/router_map_firstPage",
                    arguments: "first Router table page arg");
                //获取返回的参数并且打印在控制台
                print(arg);
              },
              child: Text("Routing table arg"),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, "/dynamic_router_page",
                    arguments: " Dynamic Routing arg");
              },
              child: Text("open Dynamic Routing  page"),
            ),
            RaisedButton(
              onPressed: () {
                //路由动画  采用自定义的PageRouteBuilder  也可以去继承PageRouteBuilder实现特定的动画
                //transitionDuration 配置动画时长
                //pageBuilder 返回一个组件  FadeTransition<动画效果> 渐变
                Navigator.push(
                    context,
                    PageRouteBuilder(
                        transitionDuration: Duration(milliseconds: 800),
                        pageBuilder: (BuildContext context,
                            Animation<double> animation,
                            Animation<double> secondaryAnimation) {
                          return FadeTransition(
                            opacity: animation,
                            child: RouterPageAnimation(),
                          );
                        }));
              },
              child: Text("open router page animation"),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, "/order_meal_index_page",
                    arguments: "");
              },child: Text("订餐系统"),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, "/button_nav_page",
                    arguments: "");
              },child: Text("底部导航栏"),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, "/stream_page",
                    arguments: "");
              },child: Text("Stream"),
            )
          ],
        ),
      ),
    );
  }
}

class SecondRouterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SecondPage")),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                //页面出栈的操作
                Navigator.pop(context);
              },
              child: Text("back Page"),
            ),
            RaisedButton(
              onPressed: () async {
                var result = await Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (BuildContext context) =>
                            ValueRouterPage("SecondRouterPage Value")));
                print(result);
              },
              child: Text("Go Value Page"),
            ),
          ],
        ),
      ),
    );
  }
}

//页面之间的值传递 push 通过构造方法参数的方式  pop通过result的方式传递（Navigator.push 有返回值接收<Future> 要使用async wait）
class ValueRouterPage extends StatelessWidget {
  //构造方法的方式
  final String arg;

  ValueRouterPage(this.arg);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("ValueRouterPage")),
      body: Center(
        child: Column(
          children: <Widget>[
            Text(arg),
            RaisedButton(
              onPressed: () {
                Navigator.pop(context, "ValueRouterPage to Value");
              },
              child: Text("value to pop"),
            )
          ],
        ),
      ),
    );
  }
}

//命名路由<使用路由表>
//路由表的启动方式    Navigator.pushNamed(context, "/second_router_page");
//参数的传递 ：
//    Navigator.of(context).pushNamed("表名字"，arguments：<携带的参数>)
//     或者 Navigator.pushNamed(context, "表名字",arguments: <携带的参数>)
//参数的接收：在build 里面获取路由表启动方式的参数
// var arg = ModalRoute.of(context).settings.arguments;
//返回参数：接受返回的参数和以前的一样 async  await

class RouterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Router App",
      theme: ThemeData(primarySwatch: Colors.blue),
      routes: {
        //定义的路由表
        "/": (context) => FirstRouterPage(),
        "/second_router_page": (context) => SecondRouterPage(),
        "/router_map_firstPage": (context) => RouterMapFirstPage(),
        "/order_meal_index_page": (context) => OrderMealIndexPage(),
        "/order_meal_record_page":(context)=>OrderMealRecordPage(),
        "/button_nav_page":(context)=>ButtonNavigation(),
        "/stream_page":(context)=>StreamPage()
      },
      initialRoute: "/",
      //配置启动的选项  功能与 home: FirstRouterPage(), 相同
      onGenerateRoute: (setting) {
        //拦截路由的生成 去动态创建一个 onGenerateRoute  优先级：routes>onGenerateRoute 只会去执行一个
        if (setting.name == "/dynamic_router_page") {
          var arg = setting.arguments;
          return MaterialPageRoute(
              builder: (context) => DynamicRouterPage(arg), settings: setting);
        }
      },
    );
  }
}

class RouterMapFirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //在build 里面获取路由表启动方式的参数
    var arg = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text("RouterMapFirstPage"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Text(arg),
            RaisedButton(
              onPressed: () {
                Navigator.pop(context, "RouterMapFirstPage arg");
              },
              child: Text("go back arg"),
            )
          ],
        ),
      ),
    );
  }
}

//拦截路由生成去动态创建一个
class DynamicRouterPage extends StatelessWidget {
  final String arg;

  DynamicRouterPage(this.arg);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DynamicRouterPage"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Text(arg),
            RaisedButton(
              onPressed: () {
                Navigator.pop(context, "DynamicRouterPage arg");
              },
              child: Text("go back arg"),
            )
          ],
        ),
      ),
    );
  }
}

//路由页面动画
class RouterPageAnimation extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("RouterPageAnimation")),
      body: Container(
        color: Colors.red,
        child: Center(
          child: RaisedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text("go back"),
          ),
        ),
      ),
    );
  }
}
